<?php
/**
 * Admin 公共控制器
 * Created by PhpStorm.
 * User: 65736
 * Date: 2018/12/12 0012
 * Time: 18:48
 */

namespace app\admin\controller;


use app\common\model\rbac\RbacModel;
use think\Config;
use think\Controller;

class AdminCommon extends Controller
{
    /* 是否登陆*/
    protected $is_login = false;

    /* 管理员编号*/
    protected $admin_id = 0;

    /* 管理员角色编号*/
    protected $admin_role_id = 0;

    /* 管理员用户数据*/
    protected $admin_data = array();

    /**
     * Rbac模型
     * @var RbacModel
     */
    protected $rbac_model = null;

    /* 是否是超级管理员*/
    protected $is_superadmin = false;

    /* 回页面级别*/
    protected $back_url_level = 1;

    /**
     * 模块名称
     * @var string
     */
    protected $module_name;

    /**
     * 控制器名称
     * @var string
     */
    protected $controller_name;

    /**
     * 方法名称
     * @var string
     */
    protected $action_name;

    /**
     * 是否是Ajax请求
     * @var bool
     */
    protected $is_ajax = false;

    /**
     * 是否是Pjax请求
     * @var bool
     */
    protected $is_pjax = false;

    /**
     * 是否是POST请求
     * @var bool
     */
    protected $is_post = false;

    /**
     * 是否是get请求
     * @var bool
     */
    protected $is_get = false;

    /**
     * 请求参数集合
     * @var array
     */
    protected $param = [];

    /**
     * 当前页数
     * @var int|string
     */
    protected $page = 1;

    /**
     * 初始化方法
     */
    protected function _initialize()
    {
        parent::_initialize();
        // 初始化属性
        $this->module_name = $this->request->module();
        $this->controller_name = $this->request->controller();
        $this->action_name = $this->request->action();
        $this->is_ajax = $this->request->isAjax();
        $this->is_post = $this->request->isPost();
        $this->is_pjax = $this->request->isPjax();
        $this->is_get = $this->request->isGet();

        // 加载项目配置
        Config::load(APP_PATH . $this->module_name . DS ."config" . CONF_EXT);
        // 初始化权限管理
        $this->rbac_model = RbacModel::getInstance();
        // 模版配置重载
        $templateConfig = Config::get('template');
        foreach ( $templateConfig as $c=>$v ) {
            $this->view->config($c, $v);
        }

        // 初始化请求参数
        $this->init_param();

        //dump(Config::get('template'));
        // 开启布局
        //$this->view->engine->layout(true);
    }

    /**
     * 初始化请求参数
     */
    private function init_param() {
        $post = $this->request->post();
        $get = $this->request->get();
        $this->param = array_merge( $post?:[], $get?:[] );
        // init page
        $varPage = Config::get("paginate.var_page");
        if ( isset( $this->param[$varPage] ) && $this->param[$varPage] > 0 ) {
            $this->page = (int)$this->param[$varPage];
        }
    }

    /**
     * 解析搜索
     */
    protected function parse_search() {
        $search = $this->param;
        // 分配搜索
        $this->assign('search', $search);
        foreach ( $search as $k => $v ) {
            if ( $v=='' || $v=='all' ) {
                unset($search[$k]);
            }
        }

        return $search;
    }




}